<?php
/**
* ResponseController
*/
class ResponseController {
	/**
	 * Cabe�alhos
	 *
	 * @access private
	 */
	private $_headers;
	
	/**
	 * Vari�veis a serem criadas para utiliza��o ap�s o forward
	 * 
	 * @access private
	 */
	private $_variables;
	
	/**
	 * Construtor
	 *
	 * @access public
	 */
	public function __construct() {
		$this->_headers = array ();
		$this->_variables = array();
		
		$this->setHeader('Expires', '0');
		$this->setHeader('Last-Modified', '0');
		$this->setHeader('Pragma', 'no-cache');
		$this->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, private, no-transform, proxy-revalidate');
		$this->setHeader('Age', '0');
	}
	
	/**
	 * Retorna cabe�alho
	 *
	 * @access public
	 */
	public function getHeader($key) {
		if (isset ( $this->_headers [$key] )) {
			return $this->_headers [$key];
		}
		return null;
	}
	
	/**
	 * Seta cabe�alho
	 *
	 * @access public
	 */
	public function setHeader($key, $value) {
		if (is_null ( $key ) | empty ( $key ) | is_bool ( $key )) {
			throw new Exception ( 'A chave n�o deve ser do tipo v�zio, booleano ou nulo! ' . __METHOD__);
		}
		$this->_headers [$key] = $value;
	}
	
	/**
	 * Remove um cabe�alho
	 *
	 * @access public
	 */
	public function unsetHeader($key) {
		if (! isset ( $this->_headers [$key] )) {
			throw new Exception ( "O cabe�alho [{$key}] n�o existe! " . __METHOD__ );
		}
		unset ( $this->_headers [$key] );
	}
	
	/**
	 * Envia os headers
	 *
	 * @access private
	 */
	private function sendHeaders() {
		foreach ( $this->_headers as $key => $value ) {
			header ( "{$key}: {$value}" );
		}
	}
	
	/**
	 * Adciona uma vari�vel a ser criada. 
	 *
	 * @access public
	 */
	public function addVariable($nome, $valor) {
		if ( !preg_match(';^[a-z_][a-z0-9_]+$;i', $nome) ) {
			throw new Exception ( 'O nome deve ser um nome v�lido de vari�vel no php! ' . __METHOD__);
		}
		$this->_variables[$nome] = $valor;
	}
	
	/**
	 * Redirecionamento
	 *
	 * @access public
	 */
	public function redirect($url) {
		if (! is_string ( $url )) {
			throw new Exception ( 'O valor deve ser do tipo string! (redirect)' . __METHOD__);
		}
		header ( "Location: {$url}" );
		die ();
	}
	
	/**
	 * Encaminha conteudo da resposta
	 *
	 * @access public
	 */
	public function forward(RequestController $request, ResponseController $response) {
		$view = $request->getAttribute ( 'view' );
               
		if (! file_exists ( './app/view/' . $view ) ) {
			throw new Exception ( "A view [{$view}] não foi encontrada! " . __METHOD__);
		}
		 
                
		if ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ) {
			ini_set('zlib.output_compression', 'on');
			$this->setHeader('Content-Encoding', 'gzip');
		}

		$this->sendHeaders();
		
		if ( !empty($this->_variables) ) {
			foreach ( $this->_variables as $nome => $valor ) {
				$$nome = $valor;
			}
		}

		ob_start();
			include $view;
		ob_end_flush ();
		
		die ();
	}
}
?>